﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SQLite
{

    /// <summary>
    /// 列的Attribute
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    public class ColumnAttribute : Attribute
    {
        /// <summary>
        /// 是否是主键
        /// </summary>
        private bool _IsPk = false;
        /// <summary>
        /// 是否可空
        /// </summary>
        private bool _NotNull = true;
        /// <summary>
        /// 列名
        /// </summary>
        private string _ColumnName = "";

        /// <summary>
        /// 字段名
        /// </summary>
        private string _FileName = "";
        /// <summary>
        /// 是否是主键
        /// </summary>
        public bool IsPk { get => _IsPk; set => _IsPk = value; }
        /// <summary>
        /// 是否可空
        /// </summary>
        public bool NotNull { get => _NotNull; set => _NotNull = value; }
        /// <summary>
        /// 列名
        /// </summary>
        public string ColumnName { get => _ColumnName; set => _ColumnName = value; }

        /// <summary>
        /// 字段
        /// </summary>
        public string FileName { get => _FileName; set => _FileName = value; }


        /// <summary>
        /// 构造函数
        /// (如果是主键，设置是否为空无效（必须不为空）)
        /// </summary>
        /// <param name="columnName">列名</param>
        /// <param name="isPk">是否是主键</param>
        /// <param name="notNull">是否可以为空</param>
        public ColumnAttribute(string columnName="", bool isPk = false, bool notNull = false)
        {
            _IsPk = isPk;
            _NotNull = isPk ? true : notNull;
            _ColumnName = columnName;
            _FileName = columnName;
        }


        public override string ToString()
        {
            return $"ColumnName:{ColumnName}\t\r\nFileName:{_FileName}\t\r\nIsPk:{IsPk}\t\r\nNotNull:{NotNull}";
        }

        public override bool Equals(object obj)
        {
            var attribute = obj as ColumnAttribute;
            return attribute != null &&
                   base.Equals(obj) &&
                   IsPk == attribute.IsPk &&
                   NotNull == attribute.NotNull &&
                   ColumnName == attribute.ColumnName &&
                   FileName == attribute.FileName;
        }

        public override int GetHashCode()
        {
            var hashCode = -115917255;
            hashCode = hashCode * -1521134295 + base.GetHashCode();
            hashCode = hashCode * -1521134295 + IsPk.GetHashCode();
            hashCode = hashCode * -1521134295 + NotNull.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ColumnName);
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FileName);
            return hashCode;
        }
    }

}
